########################################################
# Project:    Talking to the Populist Radical Right
# Task:       The script merges the data sets with the
#             scores from the CA for the Swedish parties, 
#             normalizes & annotates them
# Author:     Jan Schwalbach (21/07/2022)
########################################################

# Select legislative period and load respective data

load(file="CA_SW_ALL.Rdata")
load(file="CA_SW_MIG.Rdata")
load(file="CA_SW_ED.Rdata")

load(file="CA_SW_ALL_14.Rdata")
load(file="CA_SW_MIG_14.Rdata")
load(file="CA_SW_ED_14.Rdata")

Positions_CA_SW_ALL[,1] <- (Positions_CA_SW_ALL[,1]-min(Positions_CA_SW_ALL[,1]))/(max(Positions_CA_SW_ALL[,1])-min(Positions_CA_SW_ALL[,1]))
Positions_CA_SW_ALL[,4] <- (Positions_CA_SW_ALL[,4]-min(Positions_CA_SW_ALL[,4]))/(max(Positions_CA_SW_ALL[,4])-min(Positions_CA_SW_ALL[,4]))
Positions_CA_SW_ALL[,7] <- (Positions_CA_SW_ALL[,7]-min(Positions_CA_SW_ALL[,7]))/(max(Positions_CA_SW_ALL[,7])-min(Positions_CA_SW_ALL[,7]))

Positions_CA_SW_MIG[,1] <- (Positions_CA_SW_MIG[,1]-min(Positions_CA_SW_MIG[,1]))/(max(Positions_CA_SW_MIG[,1])-min(Positions_CA_SW_MIG[,1]))
Positions_CA_SW_MIG[,4] <- (Positions_CA_SW_MIG[,4]-min(Positions_CA_SW_MIG[,4]))/(max(Positions_CA_SW_MIG[,4])-min(Positions_CA_SW_MIG[,4]))
Positions_CA_SW_MIG[,7] <- (Positions_CA_SW_MIG[,7]-min(Positions_CA_SW_MIG[,7]))/(max(Positions_CA_SW_MIG[,7])-min(Positions_CA_SW_MIG[,7]))

Positions_CA_SW_ED[,1] <- (Positions_CA_SW_ED[,1]-min(Positions_CA_SW_ED[,1]))/(max(Positions_CA_SW_ED[,1])-min(Positions_CA_SW_ED[,1]))
Positions_CA_SW_ED[,4] <- (Positions_CA_SW_ED[,4]-min(Positions_CA_SW_ED[,4]))/(max(Positions_CA_SW_ED[,4])-min(Positions_CA_SW_ED[,4]))
Positions_CA_SW_ED[,7] <- (Positions_CA_SW_ED[,7]-min(Positions_CA_SW_ED[,7]))/(max(Positions_CA_SW_ED[,7])-min(Positions_CA_SW_ED[,7]))

CA_SW_ALL_ALL <- Positions_CA_SW_ALL[,1:3]
CA_SW_ALL_GOV <- Positions_CA_SW_ALL[,4:6]
CA_SW_ALL_OPP <- Positions_CA_SW_ALL[,7:9]

CA_SW_MIG_ALL <- Positions_CA_SW_MIG[,1:3]
CA_SW_MIG_GOV <- Positions_CA_SW_MIG[,4:6]
CA_SW_MIG_OPP <- Positions_CA_SW_MIG[,7:9]

CA_SW_ED_ALL <- Positions_CA_SW_ED[,1:3]
CA_SW_ED_GOV <- Positions_CA_SW_ED[,4:6]
CA_SW_ED_OPP <- Positions_CA_SW_ED[,7:9]

names(CA_SW_ALL_ALL)[1] <- "Position"
names(CA_SW_ALL_GOV)[1] <- "Position"
names(CA_SW_ALL_OPP)[1] <- "Position"
names(CA_SW_MIG_ALL)[1] <- "Position"
names(CA_SW_MIG_GOV)[1] <- "Position"
names(CA_SW_MIG_OPP)[1] <- "Position"
names(CA_SW_ED_ALL)[1] <- "Position" 
names(CA_SW_ED_GOV)[1] <- "Position" 
names(CA_SW_ED_OPP)[1] <- "Position" 

names(CA_SW_ALL_ALL)[2] <- "Position2"
names(CA_SW_ALL_GOV)[2] <- "Position2"
names(CA_SW_ALL_OPP)[2] <- "Position2"
names(CA_SW_MIG_ALL)[2] <- "Position2"
names(CA_SW_MIG_GOV)[2] <- "Position2"
names(CA_SW_MIG_OPP)[2] <- "Position2"
names(CA_SW_ED_ALL)[2] <- "Position2" 
names(CA_SW_ED_GOV)[2] <- "Position2" 
names(CA_SW_ED_OPP)[2] <- "Position2" 

# Calculating the difference to the challenger party

CA_SW_ALL_ALL$challenger <- CA_SW_ALL_ALL$Position[7]
CA_SW_ALL_ALL$Difference <- CA_SW_ALL_ALL$Position-CA_SW_ALL_ALL$challenger
CA_SW_ALL_ALL$Difference <- abs(CA_SW_ALL_ALL$Difference)

CA_SW_ALL_GOV$challenger <- CA_SW_ALL_GOV$Position[7]
CA_SW_ALL_GOV$Difference <- CA_SW_ALL_GOV$Position-CA_SW_ALL_GOV$challenger
CA_SW_ALL_GOV$Difference <- abs(CA_SW_ALL_GOV$Difference)

CA_SW_ALL_OPP$challenger <- CA_SW_ALL_OPP$Position[7]
CA_SW_ALL_OPP$Difference <- CA_SW_ALL_OPP$Position-CA_SW_ALL_OPP$challenger
CA_SW_ALL_OPP$Difference <- abs(CA_SW_ALL_OPP$Difference)

CA_SW_MIG_ALL$challenger <- CA_SW_MIG_ALL$Position[7]
CA_SW_MIG_ALL$Difference <- CA_SW_MIG_ALL$Position-CA_SW_MIG_ALL$challenger
CA_SW_MIG_ALL$Difference <- abs(CA_SW_MIG_ALL$Difference)

CA_SW_MIG_GOV$challenger <- CA_SW_MIG_GOV$Position[7]
CA_SW_MIG_GOV$Difference <- CA_SW_MIG_GOV$Position-CA_SW_MIG_GOV$challenger
CA_SW_MIG_GOV$Difference <- abs(CA_SW_MIG_GOV$Difference)

CA_SW_MIG_OPP$challenger <- CA_SW_MIG_OPP$Position[7]
CA_SW_MIG_OPP$Difference <- CA_SW_MIG_OPP$Position-CA_SW_MIG_OPP$challenger
CA_SW_MIG_OPP$Difference <- abs(CA_SW_MIG_OPP$Difference)

CA_SW_ED_ALL$challenger <- CA_SW_ED_ALL$Position[7]
CA_SW_ED_ALL$Difference <- CA_SW_ED_ALL$Position-CA_SW_ED_ALL$challenger
CA_SW_ED_ALL$Difference <- abs(CA_SW_ED_ALL$Difference)

CA_SW_ED_GOV$challenger <- CA_SW_ED_GOV$Position[7]
CA_SW_ED_GOV$Difference <- CA_SW_ED_GOV$Position-CA_SW_ED_GOV$challenger
CA_SW_ED_GOV$Difference <- abs(CA_SW_ED_GOV$Difference)

CA_SW_ED_OPP$challenger <- CA_SW_ED_OPP$Position[7]
CA_SW_ED_OPP$Difference <- CA_SW_ED_OPP$Position-CA_SW_ED_OPP$challenger
CA_SW_ED_OPP$Difference <- abs(CA_SW_ED_OPP$Difference)

# Annotating government/opposition debates

CA_SW_ALL_ALL$type <-"all"
CA_SW_ALL_GOV$type <-"government"  
CA_SW_ALL_OPP$type <-"opposition"
CA_SW_MIG_ALL$type <-"all"
CA_SW_MIG_GOV$type <-"government"  
CA_SW_MIG_OPP$type <-"opposition"
CA_SW_ED_ALL$type <-"all" 
CA_SW_ED_GOV$type <-"government"   
CA_SW_ED_OPP$type <-"opposition" 

CA_SW_ALL_ALL$matter <-"all"
CA_SW_ALL_GOV$matter <-"all"  
CA_SW_ALL_OPP$matter <-"all"
CA_SW_MIG_ALL$matter <-"Migration"
CA_SW_MIG_GOV$matter <-"Migration"  
CA_SW_MIG_OPP$matter <-"Migration"
CA_SW_ED_ALL$matter <-"Education" 
CA_SW_ED_GOV$matter <-"Education"   
CA_SW_ED_OPP$matter <-"Education" 

Positions_SW <- rbind(CA_SW_ALL_ALL,CA_SW_ALL_GOV,CA_SW_ALL_OPP,CA_SW_MIG_ALL,CA_SW_MIG_GOV,CA_SW_MIG_OPP,CA_SW_ED_ALL,CA_SW_ED_GOV,CA_SW_ED_OPP)
Positions_SW$Party <- Positions_SW$party
Positions_SW$country <- "SW"

# left/right parties

Positions_SW$left <- 0
Positions_SW$left[Positions_SW$Party == "MP"] <- 1
Positions_SW$left[Positions_SW$Party == "S"] <- 1
Positions_SW$left[Positions_SW$Party == "V"] <- 1

# mainstream parties

Positions_SW$mainstream <- 0
Positions_SW$mainstream[Positions_SW$Party == "M"] <- 1
Positions_SW$mainstream[Positions_SW$Party == "S"] <- 1

# government/opposition parties and legislative period

Positions_SW$year <- 2010
Positions_SW$year <- 2014

Positions_SW$Government <- "Opposition"

Positions_SW$Government[Positions_SW$Party == "M" & Positions_SW$year == 2010] <- "Government"
Positions_SW$Government[Positions_SW$Party == "KD" & Positions_SW$year == 2010] <- "Government"
Positions_SW$Government[Positions_SW$Party == "L" & Positions_SW$year == 2010] <- "Government"
Positions_SW$Government[Positions_SW$Party == "C" & Positions_SW$year == 2010] <- "Government"
Positions_SW$Government[Positions_SW$Party == "S" & Positions_SW$year == 2014] <- "Government"
Positions_SW$Government[Positions_SW$Party == "MP" & Positions_SW$year == 2014] <- "Government"

save(Positions_SW, file = "Positions_SW_CA_normal.Rdata") 
save(Positions_SW, file = "Positions_SW_CA_14_normal.Rdata")
